package com.teamwork.ZuccEcho.Controller;

import com.alibaba.fastjson.JSON;
import com.teamwork.ZuccEcho.Entity.Student;
import com.teamwork.ZuccEcho.Entity.Teacher;
import com.teamwork.ZuccEcho.Repository.LoginStuApiRepository;
import com.teamwork.ZuccEcho.Repository.LoginTeaApiRepository;
import com.teamwork.ZuccEcho.Result.ExceptionMsg;
import com.teamwork.ZuccEcho.Result.Response;
import com.teamwork.ZuccEcho.Result.ResponseData;
import com.teamwork.ZuccEcho.Service.impl.StudentServiceImpl;
import com.teamwork.ZuccEcho.Service.impl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginApiController {

    protected Response result(ExceptionMsg msg){
        return new Response(msg);
    }

    @Autowired
    private LoginTeaApiRepository loginTeaApiRepository;
    @Autowired
    private LoginStuApiRepository loginStuApiRepository;

    @Autowired
    StudentServiceImpl studentService;

    @Autowired
    TeacherServiceImpl teacherService;

    public List<Teacher> teachers;
    public List<Student> students;

    @RequestMapping(value = "/v1/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData loginByPassword(@RequestBody Map<String,String> body){
        String id=body.get("id");
        String pwd=body.get("pwd");

        if(Character.isDigit(id.charAt(0))){
            if(studentService.login(id,pwd)){
                Student student=loginStuApiRepository.findStudentByStuid(id);
                String strJSON = JSON.toJSONString(student.toString());
                return new ResponseData(ExceptionMsg.SUCCESS,strJSON);
            }
        }else {
            if(teacherService.login(id,pwd)){
                Teacher teacher=loginTeaApiRepository.findTeacherById(id);
                String strJSON = JSON.toJSONString(teacher.toString());
                return new ResponseData(ExceptionMsg.SUCCESS,strJSON);
            }
        }
        return new ResponseData(ExceptionMsg.FAILED,"密码错误");
    }

    @RequestMapping(value = "/bindPhone/v1/", method = RequestMethod.POST)
    @ResponseBody
    public Response bindPhone(@RequestBody Map<String,String> body){
        String phone=body.get("phone");
        String id=body.get("id");
        loginStuApiRepository.modifyPhoneByStuId(phone,id);
        return result(ExceptionMsg.SUCCESS);
    }

    @RequestMapping("/forgetPwd/v1/")
    @ResponseBody
    public Response forgetPwd(@RequestBody Map<String,String> body){
        //todo 验证码还没做
        String phone=body.get("phone");
        String id=body.get("id");
        String newpwd1=body.get("pwd1");
        String newpwd2=body.get("pwd2");

        if(newpwd1.equals(newpwd2)){
            loginStuApiRepository.modifyPwdByPhone(newpwd1,id,phone);
            return result(ExceptionMsg.SUCCESS);
        }else {
            return result(ExceptionMsg.FAILED);
        }
    }
    @RequestMapping("/modifyPwd/v1/")
    @ResponseBody
    public Response modifyPwd(@RequestBody Map<String,String> body){
        String oldPwd=body.get("oldpwd");
        String id=body.get("id");
        String newpwd1=body.get("pwd1");
        String newpwd2=body.get("pwd2");
        if(newpwd1.equals(newpwd2)){
            loginStuApiRepository.modifyPwdByOldPwd(newpwd1, id, oldPwd);
            return result(ExceptionMsg.SUCCESS);
        }else {
            return result(ExceptionMsg.FAILED);
        }
    }
}
